<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace ApplicationsModule\GalleryModule;

use Nette\Object;
use Nette\Web\User;

use ApplicationsModule\GalleryModule\IGalleryDriver;
use Celebrio\Core\ServiceProvider;
use ApplicationsModule\GalleryModule\LocalAlbum;
use Celebrio\Modules;
use \Celebrio\FileSystem\FileNotFoundException;
use ApplicationsModule\GalleryModule\GalleryNotFoundException;
use ApplicationsModule\GalleryModule\GalleryNotEmptyException;
/**
 * Description of Local
 *
 * @author honza
 */
class Local extends Object implements IGalleryDriver {
    
    const NAME = "local";
    const EMPTY_FOLDER = "/app/gallery/folder.png";
    const REST_LOAD = '../api/rest/load?out=image&what=';
    
    private $galleryPath;
    private $user = null;
    private $fsApi = null;
    
    
    public function __construct() {
        
    }
    
    public function loadAlbums() {
        $remote = $this->getFileSystem();
        //$remote->load($this->galleryPath);
        $galleries = $this->getGalleries();
        if(empty($galleries))
            return null;
        $content = array();
        foreach($galleries as $gallery) {
                $content[] = $remote->load($gallery->getUrl() . $gallery->getName());
        }
        $samples = array();
        foreach($content as $sample) {
            $galleryName = $sample->getName();
            $photos = $sample->getData();
            if(!empty($photos)) {
                $firstPhoto = $photos[0];
                $localAlbum = $this->createLocalAlbum($firstPhoto);
                $localAlbum->setName(ucfirst($galleryName));
                $samples[] = $localAlbum;
                /*
                $firstPhoto = $photos[0];
                $pictureUrl = $firstPhoto->getUrl() . $firstPhoto->getName();
                */
            } else {
                $firstPhoto = null;
                //$pictureUrl = self::EMPTY_FOLDER;
            }
            //$samples[] = $remote->load($firstPhoto->getUrl() . $firstPhoto->getName());
        }
        return $samples;
    }

    public function loadPhotosFromAlbum($albumPath, $userName) {
        $remote = $this->getFileSystem();
        $photos = $remote->load($albumPath)->getData();
        $result = array();
        foreach($photos as $photo) {
            $result[] = $this->createLocalAlbum($photo);
        }
        return $result;
    }
    
    public function createAlbum($name) {
        $fs = $this->getFileSystem();
        $path = $this->getGalleryPath() . $name;
        $fs->save($path, null);
    }
    
    /**
     * Deletes selected album if $force is false it will raise exception
     * which indicates that you are not allowed to delete
     * 
     * @param type $name string name of album to delete
     * @param type $force bool delete even album is NOT empty
     */
    public function deleteAlbum($name, $force = false) {
        $fs = $this->getFileSystem($this->getUser());
        $path = $this->getGalleryPath() . $name;
        if(!$force) {
            $loaded = $fs->load($path, false);
            if(!empty($loaded)) {
                throw new GalleryNotEmptyException("Gallery is not empty");
            }
        }
        $fs->delete($path);
        
    }
    
    public function deletePhoto($albumName, $photoName) {
        throw new \NotImplementedException;
    }

    public function checkAvailability() {
        $modules = ServiceProvider::getAllServiceNames(Modules::FS);
        if (!empty($modules))
            return true;
        return false;
    }
    
    public function setUser(User $user) {
        $this->user = $user;
    }
    
    public function getUser() {
        return $this->user;
    }
    
    public function getGalleries() {
        $remote = $this->getFileSystem();
        try {
            $galleries = $remote->load($this->getGalleryPath())->getData();
        } catch(FileNotFoundException $e) {
            return array();
        }
        $result = array();
        foreach($galleries as $gallery) {
            $result[] = $gallery;
        }
        return $result;
        
    }
    
    private function getGalleryPath() {
        return "/usr/" . $this->getUserName() . "/gallery/";
    }
    
    private function getUserName() {
        return $this->user->getIdentity()->name;
    }
    
    private function createLocalAlbum($photo) {
        $url = $photo->getUrl() . $photo->getName();
        $localAlbum = new LocalAlbum();
        $localAlbum->setContentUrl(self::REST_LOAD . $url);
        $localAlbum->setThumbnail($localAlbum->getContentUrl());
        $localAlbum->setName($photo->getName());
        $localAlbum->setService(self::NAME);
        $localAlbum->setTitle($photo->getName());
        $localAlbum->setIdUser($photo->getFileInfo()->getOwnerName());
        $localAlbum->setId($photo->getUrl());
        return $localAlbum;
    }
    
    public function getGalleryNames() {
        $galleries = $this->getGalleries();
        $names = array();
        foreach($galleries as $gallery) {
            $name = $gallery->getName();
            $info = array(
                "name" => $name,
                "id" => $name
                );
            $names[] = $info;
        }
        return $names;
    }
    
    public function savePhoto($albumName, $photoName, $data, $mimetype) {
        $fs = $this->getFileSystem();
        $path = $this->getGalleryPath() . $albumName . "/" . $photoName;
        $fs->save($path, $data, $mimetype);
    }
    
    private function getFileSystem() {
        if($this->fsApi == null) {
            $this->fsApi = ServiceProvider::getService(Modules::FS);
        }
        
        return $this->fsApi;
    }
    
    
    
}